package com.kgc.java.javaoop.chapter06.c6project;

import sun.java2d.pipe.SpanIterator;

public class Function {
    static User users[] = new User[3];
    static Employee employees[] = new Employee[10];
    static User user = null;
    static Employee employee = null;

    /**
     * 初始化
     */
    static{
        user = new User("zhangsan","123456");
        users[0] = user;
        user = new User("lisi","123456");
        users[1] = user;
        user = new User("wangwu","123456");
        users[2] = user;
        employee = new Employee(1,"蔡徐坤",1000);
        employees[0] = employee;
        employee = new Employee(2,"51凡",2000);
        employees[1] = employee;
        employee = new Employee(3,"曹智",250);
        employees[2] = employee;
        employee = new Employee(4,"代孕飞",1250);
        employees[3] = employee;
        employee = new Employee(5,"张浩",2250);
        employees[4] = employee;
    }

    /**
     * 登陆方法
     * @param id
     * @param pwd
     * @return
     */
    boolean flag = false;
    public boolean login(String id,String pwd) {
        flag = false;
        for (int i = 0; i < users.length; i++) {
            if (users[i]!=null){
                if (users[i].getuName().equals(id)&&users[i].getuPwd().equals(pwd)){
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }
    /**
     * 查询所有员工信息
     */
    public void printAll(){
        for (int i = 0; i < employees.length; i++) {
            if (employees[i]!=null){
                employees[i].print();
            }
        }
    }
    /**
     * 按工号查询员工信息
     */
    public Employee printByNo(int eNo){
        for (int i = 0; i < employees.length; i++) {
            if (employees[i]!=null){
                if (employees[i].geteNo()==eNo){
                    employee = employees[i];
                    break;
                }else{
                    employee=null;
                }
            }
        }
        return employee;
    }
    /**
     * 添加员工信息
     */
    public int addEmployee(Employee employee){
        int result = 0;
        for (int i = 0; i < employees.length; i++) {
            if (employees[i]!=null){
                if (employee.geteNo()==employees[i].geteNo()) {
                    result = 2;
                    break;
                }
            }
            if (employees[i]==null){
                employees[i] = employee;
                result = 1;
                break;
            }
        }
        return result;
    }
    /**
     *修改员工 工资
     */
    public boolean updateMony(int eNo,double money){
        flag =false;
        for (int i = 0; i < employees.length; i++) {
            if (employees[i]!=null){
                if (employees[i].geteNo()==eNo){
                    employees[i].seteMoney(money);
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }
    /**
     * 删除员工
     */
    public boolean deleteEmployee(int eNo){
        flag = false;
        for (int i = 0; i < employees.length; i++) {
            if (employees[i]!=null){
                if (employees[i].geteNo()==eNo){
                    employees[i]=null;
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }
}
